
package com.feib.ebilling.job.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.ebilling.job.dao.BillFeedbackDao;
import com.feib.ebilling.job.util.StringUtils;


/**
 * @title (#)BillFeedbackService.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/10 create by Jimmy Liu<br>
 */
@Service("billFeedbackService")
public class BillFeedbackService extends BaseService  {
    
    @Resource(name="billFeedbackDao")
    private BillFeedbackDao billFeedbackDao;     
   
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroupPK(String GroupNo) {
        return this.billFeedbackDao.queryGroupPKByGroupNo(GroupNo);
    }
    
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroups() {
        return this.billFeedbackDao.queryALLGroup();
    }
    
    @SuppressWarnings("finally")
    /* 學校繳費銷帳檔規格 */
	public String writeContentSchool(String groupNo, String groupPK, String processDate)
    {
    	StringBuffer sbf = new StringBuffer();    	
    	try{	    	    
	    	/* 首筆 */
    		sbf.append("1");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("L", " ", 99,""));
	    	sbf.append("\r\n");
	    	/* 明細 */
	    	List<Map<String, Object>> payments = this.billFeedbackDao.queryWriteOffByGroupNo(groupPK, processDate);
	    	long total = 0;
	    	long amount = 0;        	
	    	for(Map<String, Object> payment : payments)
	        {
	    		sbf.append("2");
	    		sbf.append(StringUtils.PadString("L", " ", 16, (String)payment.get("VIRTUAL_ACCOUNT")));
	    		sbf.append(StringUtils.PadString("L", " ", 10, (String)payment.get("PAYER_ID")));	    		    		
	    		sbf.append(StringUtils.PadStringChinese("L", " ", 20, (String)payment.get("PAYER_NAME")));        		
	    		sbf.append(StringUtils.PadString("L", " ", 3, (String)payment.get("ACADEMIC_YEAR")));
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("SEMESTER")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("TX_DATE")));
	    		String MyTime = (String)payment.get("TX_TIME");	    		
	    		sbf.append(StringUtils.PadString("L", " ", 8, MyTime.substring(0,2) + MyTime.substring(3,5)
	    				                                    + MyTime.substring(6,8) + MyTime.substring(9,11)));
	    		BigDecimal bd = (BigDecimal) payment.get("AMOUNT");
	    		/* 9(10)V99 */
	    		sbf.append(StringUtils.PadString("R", "0", 10, bd.toString()) + "00");
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("CHANNEL")));
	    		/* 交易序號 */
	    		bd = (BigDecimal) payment.get("WRITE_OFF_PAYMENT_PK");
	    		/* 最長19位 NUMERIC(19,0)*/
	    		String stanID = bd.toString();	    		
	    		sbf.append(StringUtils.PadString("R", "0", 20, stanID));	    		
	    		sbf.append(StringUtils.PadString("L", " ", 20,""));
	    		total++;
	    		amount = amount + Long.parseLong(bd.toString());
	    		sbf.append("\r\n");
	        }
	    	logger.info("銷帳筆數：{}" , String.valueOf(total));
	    	logger.info("銷帳金額：{}" , String.valueOf(amount));
	    	/* 結尾 */
	    	sbf.append("3");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("R", "0", 10, String.valueOf(total)));
	    	/* 9(14)V99 */
	    	sbf.append(StringUtils.PadString("R", "0", 14, String.valueOf(amount)) + "00");	    	
	    	sbf.append(StringUtils.PadString("L", " ", 73,""));
	    	sbf.append("\r\n");    	
    	 }catch (Exception e) {
            logger.error("[錯誤]" + e.getMessage(), e);
         }
         finally{
            return sbf.toString();            
         }
    }
    
    @SuppressWarnings("finally")
    /* 一般代收繳費銷帳檔規格 */
	public String writeContentGeneral(String groupNo, String groupPK, String processDate)
    {
    	StringBuffer sbf = new StringBuffer();    	
    	try{	    	    
	    	/* 首筆 */
    		sbf.append("1");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("L", " ", 99,""));
	    	sbf.append("\r\n");
	    	/* 明細 */
	    	List<Map<String, Object>> payments = this.billFeedbackDao.queryWriteOffByGroupNo(groupPK, processDate);
	    	long total = 0;
	    	long amount = 0;        	
	    	for(Map<String, Object> payment : payments)
	        {
	    		sbf.append("2");
	    		sbf.append(StringUtils.PadString("L", " ", 16, (String)payment.get("VIRTUAL_ACCOUNT")));
	    		sbf.append(StringUtils.PadString("L", " ", 10, (String)payment.get("PAYER_ID")));	    		    		
	    		sbf.append(StringUtils.PadStringChinese("L", " ", 68, (String)payment.get("PAYER_NAME")));        		
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("TX_DATE")));	    	
	    		BigDecimal bd = (BigDecimal) payment.get("AMOUNT");
	    		/* 9(10)V99 */
	    		sbf.append(StringUtils.PadString("R", "0", 10, bd.toString()));
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("CHANNEL")));	    			    		
	    		sbf.append(StringUtils.PadString("L", " ", 6,""));
	    		total++;
	    		amount = amount + Long.parseLong(bd.toString());
	    		sbf.append("\r\n");
	        }
	    	logger.info("銷帳筆數：{}" , String.valueOf(total));
	    	logger.info("銷帳金額：{}" , String.valueOf(amount));
	    	/* 結尾 */
	    	sbf.append("3");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("R", "0", 10, String.valueOf(total)));
	    	/* 9(14)V99 */
	    	sbf.append(StringUtils.PadString("R", "0", 14, String.valueOf(amount)) + "00");	    	
	    	sbf.append(StringUtils.PadString("L", " ", 73,""));
	    	sbf.append("\r\n");    	
    	 }catch (Exception e) {
            logger.error("[錯誤]" + e.getMessage(), e);
         }
         finally{
            return sbf.toString();            
         }
    }
	
}
